package com;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import mail.SendMail;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.connection.jdbconnection;

public class Register extends HttpServlet {
	private static final long serialVersionUID = 1L;

	static Connection conn = null;

	PreparedStatement pst = null;
	ResultSet rs = null;
	String firstName;
	String lastName;
	String userName;
	String password;
	String address;
	String contactno;
	String doj;
	String doj1;
	String email;
	String image;
	String Eid;
	String id;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
	
		doPost(request, response);
		
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		
		jdbconnection db = new jdbconnection();
		Statement st = db.jdbConnection();
		

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		String path = getServletContext().getRealPath("photo");

		HttpSession session = request.getSession();

		boolean isMultipart = ServletFileUpload.isMultipartContent(request);
		if (!isMultipart) {
			
		} else {
			
			FileItemFactory factory = new DiskFileItemFactory();
			ServletFileUpload upload = new ServletFileUpload(factory);
			List items = null;
			try {
				items = upload.parseRequest(request);
			} catch (FileUploadException e) {
				e.printStackTrace();
			}
			Iterator itr = items.iterator();
			while (itr.hasNext()) {
				FileItem item = (FileItem) itr.next();
				if (item.isFormField()) {
					String name = item.getFieldName();

					String value = item.getString();
					System.out.println(name + value);
					if (name.equals("firstName")) {
						firstName = value;
						System.out.println(firstName);
					}
					if (name.equals("lastName")) {
						lastName = value;
						System.out.println(lastName);
					}
					if (name.equals("userName")) {
						userName = value;
						System.out.println(userName);
					}
					if (name.equals("password")) {
						password = value;
						System.out.println(password);
					}
					if (name.equals("address")) {
						address = value;
						System.out.println(address);
					}
					if (name.equals("contactno")) {
						contactno = value;
						System.out.println(contactno);
					}
					if (name.equals("email")) {
						email = value;
						System.out.println(email);
					}
					if (name.equals("doj")) {
						doj = value;
						System.out.println(doj);
					}

				} else {
					try {
						String itemName = item.getName();
						String name[] = itemName.split("\\.");
						Random random = new Random();
						int z = random.nextInt(23000);
						String name1 = name[0] + z;
						image = name1 + "." + name[1];
						File savedFile = new File(path + "/" + image);
						image = "photo/" + image;
						item.write(savedFile);

						String m=doj.substring(5, 7);
						System.out.println(m);
						String ss=doj.substring(2, 4);
						System.out.println(ss);
						String d=doj.substring(8, 10);
						System.out.println(d);
						 doj1=d+"-"+m+"-"+ss;
						long x=15;
						long y=29;
						
						
						ResultSet set = st.executeQuery("select id from register ");

						if (!set.next()) {
							id = "HIG"+m+ss+x;
							System.out.println("id for new "+id);
						} else if (!set.next()) {
							id = "HIG"+m+ss+y;
						} else {
							set.last();
							String s = set.getString(1);
							System.out.println(s);
							set.previous();
							System.out.println(set.getString(1));
							String s2 = set.getString(1);
							
						long temp = x * 5 + y-2;
						id = "HIG"+m+ss+ temp;
							Eid = id;
						}

					} catch (SQLException e1) {
						
						System.out.println("SQL Exception.");
						e1.printStackTrace();
					} catch (Exception e) {
						
						System.out.println("Exception.");
						e.printStackTrace();
					}
					String query = "insert	into register values('"
							+ id
							+ "','"
							+ firstName
							+ "','"
							+ lastName
							+ "','"
							+ userName
							+ "','"
							+ password
							+ "','"
							+ address
							+ "','"
							+ contactno
							+ "','"
							+ email
							+ "','"
							+ doj1
							+ "','"
							+ image + "','" + 0 + "')";

					System.out.println(query);
					System.out.println(Eid);
					try {
						System.out.println(st);
						int a = st.executeUpdate(query);
						if (a == 1) {
							String sub = "Registered Successfully in Hi rel info genisis ";
							String msg = "Dear "
									+ firstName
									+ " Thank you for being the Faculty of the Hi-rel info genisis your Employee ID is :"
									+ id + ", For any kind of information.";
							SendMail mail = new SendMail();
							mail.sendMessageToMail(email, sub, msg);
							// HttpSession session=request.getSession();
							session.setAttribute("Eid", Eid);
							session.setAttribute("sname", firstName);
							System.out.println(Eid);

							System.out.println("User Detail Submit Successfully.");

							session.setAttribute("error", new Object());
							response.sendRedirect("Register.jsp");


						}
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

			}

		}

	}

}
